import React, { useCallback, useEffect, useLayoutEffect, useState } from 'react';
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UploadOutlined,
  UserOutlined,
  VideoCameraOutlined,
} from '@ant-design/icons';
import { Button, Layout, Menu, Select, theme } from 'antd';
import { Outlet, useNavigate } from 'react-router-dom';
import { homeRoutes } from './router';
import i18n from './i18n/i18n';

const { Header, Sider, Content } = Layout;

const App: React.FC = () => {
  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();

  // 定义路由
  const navigate = useNavigate()
  const [item, setItem] = useState([
    {
      key: '1',
      icon: <UserOutlined />,
      label: 'nav 1',
    },
  ])

  const index = useCallback((routes: any[]) => {
    const items = routes.map((item) => {
      const res: any = {}
      res.key = item.path
      if (item.icon) {
        res.icon = item.icon
      }
      res.label = item.label

      return res
    })
    return items
  }, [])

  useLayoutEffect(() => {
    setItem(index(homeRoutes))
  }, [])

  const handleRouter = useCallback((obj) => {
    navigate(obj.key)
  }, [])

  // 中英文切换
  const values = localStorage.getItem('i18n') || i18n.language || 'zh'
  const handleChange = (value: string) => {
    i18n.changeLanguage(value);
    localStorage.setItem('i18n', value);
  };

  // 日夜间切换
  const [the, setThe] = useState( localStorage.getItem('newTheme') || 'light')
  const handleChanges = (value: string) => {
    const themeData = document.documentElement
    themeData.setAttribute('data-theme', value)
    localStorage.setItem('newTheme', value)
    setThe(value)
  };

  useEffect(() => {
    setThe(the)
  }, [])

  return (
    <Layout style={{ height: '100vh' }}>
      <Sider trigger={null} collapsible collapsed={collapsed}>
        <div className="demo-logo-vertical" />
        <Menu
          theme="dark"
          mode="inline"
          defaultSelectedKeys={['/']}
          items={item}
          onClick={handleRouter}
        />
      </Sider>
      <Layout>
        <Header style={{ padding: 0, background: colorBgContainer }}>
          <div>
            <Button
              type="text"
              icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
              onClick={() => setCollapsed(!collapsed)}
              style={{
                fontSize: '16px',
                width: 64,
                height: 64,
              }}
            />
            {/* 中英文切换 */}
            <Select
              defaultValue={values}
              style={{ width: 120 }}
              onChange={handleChange}
              options={[
                { value: 'zh', label: '中文' },
                { value: 'en', label: 'English' }
              ]}
            />
            {/* 日夜间切换 */}
            <Select
              defaultValue={the}
              style={{ width: 120 }}
              onChange={handleChanges}
              options={[
                { value: 'light', label: '日间' },
                { value: 'dark', label: '夜间' }
              ]}
            />
          </div>
        </Header>
        <Content
          style={{
            margin: '24px 16px',
            padding: 24,
            minHeight: 280,
            background: colorBgContainer,
            borderRadius: borderRadiusLG,
          }}
        >
          <Outlet></Outlet>
        </Content>
      </Layout>
    </Layout>
  );
};

export default App;